/*
 * Copyright (c) 2006-2021, Eywen team
 *
 * SPDX-License-Identifier: BSD
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-09-11     Eywen        the first version
 */

#include <rtthread.h>

#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

#include "common.h"
#include "files.h"
#include "ringbuff.h"
#include "dgus_lcd.h"
#include "serial.h"

struct dfs_fd s_fd;

ringbuff_t *g_gcode_ringbuff;

static rt_thread_t uart_send_tid = RT_NULL;

/* 线程1的入口函数 */
static void thread1_entry(void *parameter)
{
    ringbuff_t *gcode_buff;
    gcode_buff = g_gcode_ringbuff;
    int timeout;
    int need_send;
    uint8_t send_buff[64];
    int ringbuff_used;
    while (1)
    {
        while(((ringbuff_used = ringbuff_used_count(gcode_buff)) < 64) &&
                (timeout++ < 3)){
            rt_thread_mdelay(1);
        }
        need_send = (ringbuff_used > 64)?64:ringbuff_used;

        for(int i = 0; i < need_send; i++){
            ringbuff_pop(gcode_buff, &send_buff[i]);
        }
    }
}

#define SAMPLE_UART_NAME       "uart1"      /* 串口设备名称 */

rt_device_t serial;

/* lcd 串口綫程主體 */
static void lcd_serial_thread_entry(void *parameter)
{
    dgus_ui_setup();
    while (1){
        dgus_task_handle();
        //rt_thread_mdelay(20);
    }
}

/* 初始化lcd串口綫程 */
static int uart_lcd_thread_init(void)
{
    rt_err_t ret = RT_EOK;

    /* 查找串口设备 */
    serial = rt_device_find(SAMPLE_UART_NAME);
    if (!serial){
        rt_kprintf("find %s failed!\n", SAMPLE_UART_NAME);
        return RT_ERROR;
    }

    /* 设置串口速率为115200 */
    struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
    config.baud_rate = 115200;
    rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);

    /* 以读写及中断接收方式打开串口设备 */
    rt_device_open(serial, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX);

    /* 创建 serial 线程 */
    rt_thread_t thread = rt_thread_create("serial", lcd_serial_thread_entry,
                                                RT_NULL, 4096, 25, 10);
    /* 创建成功则启动线程 */
    if (thread != RT_NULL){
        rt_thread_startup(thread);
    }
    else{
        ret = RT_ERROR;
    }

    return ret;
}


int main(void)
{
    rt_thread_mdelay(1000);
    serial_thread_init(NULL);
    while(1){
        //debug_error("Insufficient system memory");
        while(refresh_file_names("/") == 0){
            rt_thread_mdelay(1000);
        }
        uart_lcd_thread_init();
        rt_thread_mdelay(100);
        gui_into_print_list(NULL);
        while(1){
            rt_thread_mdelay(1000);;
        }
    }
    g_gcode_ringbuff = ringbuff_create(sizeof(uint8_t), 4096);
    if(g_gcode_ringbuff == NULL){
        debug_error("Insufficient system memory");
        while(1);
    }
    while(1){
        rt_thread_mdelay(1000);
        debug_info("rt - thread\r\n");
    }
    while (1)
    {
        list_cur_dir_files("/");

        if(dfs_file_open(&s_fd, get_cur_dif_file_name(0), O_RDONLY) < 0){
            debug_info("open file %s error", get_cur_dif_file_name(0));
            continue;
        }
        debug_info("open file %s ok", get_cur_dif_file_name(0));
        int read_bytes;
        uint8_t buff[512];
        int idx = 0;
        do{
            memset(buff, 0, sizeof(buff));
            read_bytes = dfs_file_read(&s_fd, buff, sizeof(buff));
            while(ringbuff_remin(g_gcode_ringbuff) < read_bytes){
                rt_thread_yield();
            }
            for(int i = 0; i < read_bytes; i++){
                ringbuff_push(g_gcode_ringbuff, &buff[i]);
            }
        }while(read_bytes > 0);
        dfs_file_close(&s_fd);
    }

    return RT_EOK;
}

